package com.t2t.top.action.$21visitor.ext;

import java.lang.reflect.Method;
import java.util.Collection;

public class PrintVisitor implements ReflectiveVisitor {
    public void visitCollection(Collection collection) {
        System.out.println(collection);
    }

    public void visitString(String string) {
        System.out.println("'" + string + "'");
    }

    public void visitFloat(Float f) {
        System.out.println(f);
    }

    public void visitObject(Object o) {
        System.out.println(o.toString());
    }

    public void visit(Object object) {
        try {
            Method method = getMethod(object.getClass());
            method.invoke(this, new Object[]{object});
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    protected Method getMethod(Class z) throws NoSuchMethodException {
        String method = z.getName();
        method = "visit" + method.substring(method.lastIndexOf('.') + 1);
        Method m = null;
        try {
            m = this.getClass().getMethod(method, new Class[]{z});
        } catch (Exception e) {
            m = this.getClass().getMethod("visitObject", new Class[]{Object.class});
        }
        return m;
    }
}